package com.gjs.cd.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gjs.cd.mapper.CdYearSubjectCodeMapper;
import com.gjs.cd.pojo.dto.YearSubjectCodeReq;
import com.gjs.cd.pojo.entity.CdCodeValidTerm;
import com.gjs.cd.pojo.entity.CdYearSubjectCode;
import com.gjs.cd.pojo.vo.YearSubjectCodeVo;
import com.gjs.cd.service.ICdCodeValidTermService;
import com.gjs.cd.service.ICdYearSubjectCodeService;
import com.gjs.common.microservice.api.IResourceMicroService;
import com.gjs.common.pojo.dto.request.PageQuery;
import com.gjs.common.pojo.dto.resource.TypeDTO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import static java.util.stream.Collectors.toMap;

/**
 * @author zhongwenguang
 * @date 2022-3-11 14:58
 */
@Service
public class CdYearSubjectCodeServiceImpl extends ServiceImpl<CdYearSubjectCodeMapper, CdYearSubjectCode> implements ICdYearSubjectCodeService {

    @DubboReference
    private IResourceMicroService iResourceMicroService;

    @Autowired
    private ICdCodeValidTermService iCdCodeValidTermService;

    @Override
    public IPage<YearSubjectCodeVo> getList(PageQuery pageQuery, YearSubjectCodeReq yearSubjectCodeReq) {
//        List<CdCodeValidTerm> CdCodeValidTermList = iCdCodeValidTermService.list();
        IPage<CdYearSubjectCode> yearSubjectCodePage = this.page(pageQuery.buildPage(),this.buildWrapper(yearSubjectCodeReq));
        IPage<YearSubjectCodeVo> yearSubjectCodeVoIPage = yearSubjectCodePage.convert(u -> {
            YearSubjectCodeVo v = new YearSubjectCodeVo();
            BeanUtil.copyProperties(u,v);
            return v;
        });
        if (yearSubjectCodeVoIPage.getRecords().size()>0){
            //获取学科名称，通过微服务获取
            List<TypeDTO> subjectsList = iResourceMicroService.listSubjects();
            Map<Long,String> subjectMap = subjectsList.stream().collect(toMap(TypeDTO::getKey,TypeDTO::getText));
//            Map<Long,Integer> cdCodeValidTermMap = CdCodeValidTermList.stream().collect(toMap(CdCodeValidTerm::getId,CdCodeValidTerm::getCodeYear));
            yearSubjectCodeVoIPage.convert(e->{
                e.setSubjectName(subjectMap.get(e.getSubjectId()));
//                e.setCodeYear(cdCodeValidTermMap.get(e.getValidTermId()));
                return e;
            });
        }
        return yearSubjectCodeVoIPage;
    }

    @Override
    public Wrapper<CdYearSubjectCode> buildWrapper(YearSubjectCodeReq req) {
        LambdaQueryWrapper<CdYearSubjectCode> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(req.getValidTermId() != null, CdYearSubjectCode::getValidTermId,req.getValidTermId());
        wrapper.eq(!StrUtil.isEmpty(req.getSubjectCode()), CdYearSubjectCode::getSubjectCode,req.getSubjectCode());
        wrapper.eq(req.getSubjectId() != null, CdYearSubjectCode::getSubjectId,req.getSubjectId());

        return wrapper;
    }

    @Override
    public List<CdYearSubjectCode> getCodeList() {
        QueryWrapper<CdYearSubjectCode> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("subjectId");
        queryWrapper.select("subjectCode");
        return this.list(queryWrapper);
    }
}
